package util;

import java.sql.*;


public class DButils {
    private static Connection conn = null;

    public static Connection getConn(){
        PropertiesUtil.loadFile("jdbc.properties");
        String driver = PropertiesUtil.getPropertyValue("driver");
        String url = PropertiesUtil.getPropertyValue("url");
        String username= PropertiesUtil.getPropertyValue("username");
        String password = PropertiesUtil.getPropertyValue("password");
 
 
         try {
            Class.forName(driver);
            conn = DriverManager.getConnection(url,username,password);
 
 
         } catch (ClassNotFoundException e) {
             e.printStackTrace();
         } catch (SQLException e) {
             e.printStackTrace();
            close();
         }
         return conn;
     }

    public static void close(Statement stat, Connection conn)
            throws SQLException {
        if (stat != null && !stat.isClosed()) {
            stat.close();
        }
        if (conn != null && !conn.isClosed()) {
            conn.close();
        }
    }

    public static void close(ResultSet rst, Statement stat, Connection conn)
            throws SQLException {
        if (stat != null && !stat.isClosed()) {
            stat.close();
        }
        if (conn != null && !conn.isClosed()) {
            conn.close();
        }
    }

    public static void close(ResultSet rst, PreparedStatement pstat)throws SQLException {
        // TODO Auto-generated method stub
        if (pstat != null && !pstat.isClosed()) {
            pstat.close();
        }
    }

    public static void close(Connection conn, PreparedStatement ps) throws SQLException {
        // TODO Auto-generated method stub
        if (conn != null && !conn.isClosed()) {
            conn.close();
        }
        if (ps != null && !ps.isClosed()) {
            ps.close();
        }
    }

    public static void close(){
        try {
            conn.close();
         } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    public static void close(Connection con, Statement st, Statement stt) throws SQLException {
        if(con!=null&&!con.isClosed())
            con.close();
        if(st!=null&&!st.isClosed())
            st.close();
        if(stt!=null&&!stt.isClosed())
            stt.close();
    }

    public static void main(String [] args){
            Connection con=getConn();
            close();
    }
}
